package com.graduation.vaccine.controller.web;

import com.graduation.vaccine.service.IAppointmentService;
import com.graduation.vaccine.service.UserService;
import com.graduation.vaccine.vo.RespBean;
import com.graduation.vaccine.vo.RespBeanEnum;
import com.graduation.vaccine.vo.UpdateAppointmentStatusVo;
import com.graduation.vaccine.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/1 20:03
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/order")
@Api(tags = "web——订单模块")
public class WebAppointmentController {

    @Autowired
    private UserService userService;
    @Autowired
    private IAppointmentService appointmentService;

    @ApiOperation("5/01——01：获取疫苗列表")
    @ResponseBody
    @PostMapping("/list")
    public RespBean getList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && ((userVo.getAction().equals("system_administrator")))){
//                String departmentCode=userVo.getDepartmentCode();
                return appointmentService.getListByCode(null);
            }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                String departmentCode=userVo.getDepartmentCode();
                return appointmentService.getListByCode(departmentCode);
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01——01:修改订单状态")
    @ResponseBody
    @PostMapping("/update/status")
    public RespBean updateStatus(@RequestBody UpdateAppointmentStatusVo updateAppointmentStatusVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateAppointmentStatusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                return appointmentService.updateStatus(updateAppointmentStatusVo.getId(),updateAppointmentStatusVo.getStatus(),updateAppointmentStatusVo.getCurrentStatus());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
